package com.bhs.sansong.ui.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansong.adapter.CityAdapter;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.bean.CityBean;
import com.bhs.sansong.bean.CityDataBean;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

public class CityPickerReuse implements NetCallBack {

    private EditText mEtSearch;
    private RecyclerView mRvProvince;
    private CityAdapter provinceAdapter;
    private List<CityBean> provinceList = new ArrayList<>();
    private RecyclerView mRvCity;
    private CityAdapter cityAdapter;
    private List<CityBean> cityList = new ArrayList<>();
    ;
    private RecyclerView mRvCounty;
    private CityAdapter countyAdapter;
    public List<CityBean> countyList = new ArrayList<>();
    private int cityIndex = 0;
    private int countIndex = 0;
    private int provinceIndex = 0;
    private boolean isMultipleChoice;
    private String city;

    public CityPickerReuse(BaseActivity activity, RecyclerView mRvProvince
            , RecyclerView mRvCity, RecyclerView mRvCounty, EditText mEtSearch, boolean isMultipleChoice) {
        this.mRvCity = mRvCity;
        this.mRvProvince = mRvProvince;
        this.mRvCounty = mRvCounty;
        this.mEtSearch = mEtSearch;
        this.isMultipleChoice = isMultipleChoice;
        activity.networkRequest(activity.netApi.getCityData(), this);
        mEtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //点击搜索要做的操作
                    dispose(mEtSearch.getText().toString());
                    return true;
                }
                return false;
            }
        });

        mEtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                dispose(editable.toString());
            }
        });

        cityAdapter = new CityAdapter(cityList, 2, isMultipleChoice);
        mRvCity.setAdapter(cityAdapter);
        mRvCity.setLayoutManager(new LinearLayoutManager(activity));

        countyAdapter = new CityAdapter(countyList, 3, isMultipleChoice);
        mRvCounty.setAdapter(countyAdapter);
        mRvCounty.setLayoutManager(new LinearLayoutManager(activity));

        provinceAdapter = new CityAdapter(provinceList, 1, isMultipleChoice);
        mRvProvince.setAdapter(provinceAdapter);
        mRvProvince.setLayoutManager(new LinearLayoutManager(activity));
        provinceAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                provinceList.get(provinceIndex).setCheck(false);
                provinceList.get(position).setCheck(true);
                provinceAdapter.notifyDataSetChanged();
                provinceIndex = position;
                provinceSelect(position);
            }
        });

        cityAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                cityList.get(cityIndex).setCheck(false);
                cityList.get(position).setCheck(true);
                cityAdapter.notifyDataSetChanged();
                cityIndex = position;
                countySelect(position);
            }
        });

        countyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (!isMultipleChoice) {
                    countyList.get(countIndex).setCheck(false);
                    countyList.get(position).setCheck(true);
                    countIndex = position;
                } else {
                    if (countyList.get(position).getCity_name().equals("全市")) {
                        countyList.get(position).setCheck(!countyList.get(position).isCheck());
                        for (CityBean cityBean : countyList) {
                            cityBean.setCheck(countyList.get(position).isCheck());
                        }
                    } else {
                        countyList.get(position).setCheck(!countyList.get(position).isCheck());
                        boolean isHas = true;
                        for (CityBean cityBean : countyList) {
                            if (!cityBean.isCheck()) {
                                isHas = false;
                                break;
                            }
                        }
                        countyList.get(0).setCheck(isHas);
                    }
                }
                countyAdapter.notifyDataSetChanged();
            }
        });
    }

    public void setCity(String city) {
        this.city = city;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void dispose(String search) {
        boolean isFind = false;
        int cityIndex = 0;
        int countIndex = 0;
        int provinceIndex = 0;
        for (int i = 0; i < provinceList.size(); i++) {
            if (provinceList.get(i).getCity_name().contains(search)) {
                provinceIndex = i;
                break;
            }
            for (int j = 0; j < provinceList.get(i).getChildren().size(); j++) {
                if (provinceList.get(i).getChildren().get(j).getCity_name().contains(search)) {
                    isFind = true;
                    cityIndex = j;
                    break;
                }

                for (int k = 0; k < provinceList.get(i).getChildren().get(j).getChildren().size(); k++) {
                    if (provinceList.get(i).getChildren().get(j).getChildren().get(k).getCity_name().contains(search)) {
                        isFind = true;
                        countIndex = k;
                        break;
                    }
                }
                if (isFind) {
                    cityIndex = j;
                    break;
                }
            }
            if (isFind) {
                provinceIndex = i;
                break;
            }
        }
        provinceList.get(this.provinceIndex).setCheck(false);
        cityList.get(this.cityIndex).setCheck(false);
        if (!isMultipleChoice) {
            countyList.get(this.countIndex).setCheck(false);
        } else {
            for (CityBean cityBean : countyList) {
                cityBean.setCheck(false);
            }
        }

        this.provinceIndex = provinceIndex;
        this.cityIndex = cityIndex;
        this.countIndex = countIndex;
        cityList.clear();
        cityList.addAll(provinceList.get(provinceIndex).getChildren());
        countyList.clear();
        countyList.addAll(cityList.get(cityIndex).getChildren());

        provinceList.get(provinceIndex).setCheck(true);
        cityList.get(cityIndex).setCheck(true);
        countyList.get(countIndex).setCheck(true);

        provinceAdapter.notifyDataSetChanged();
        cityAdapter.notifyDataSetChanged();
        countyAdapter.notifyDataSetChanged();
        //滑动到合适位置
        mRvProvince.scrollToPosition(provinceIndex);
        mRvProvince.smoothScrollToPosition(provinceIndex);

        mRvCounty.scrollToPosition(countIndex);
        mRvCounty.smoothScrollToPosition(countIndex);

        mRvCity.scrollToPosition(cityIndex);
        mRvCity.smoothScrollToPosition(cityIndex);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        CityDataBean cityBean = new Gson().fromJson(json, CityDataBean.class);
        if (cityBean.isStatus()) {
            mEtSearch.setVisibility(View.VISIBLE);
            provinceList.addAll(cityBean.getData().getCity_china());
            provinceList.get(0).setCheck(true);
            provinceSelect(0);
            provinceAdapter.notifyDataSetChanged();
            if (!TextUtils.isEmpty(city)) {
                dispose(city);
            }
        } else {
            ToastUtil.show(cityBean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }

    @SuppressLint("NotifyDataSetChanged")
    private void provinceSelect(int index) {
        cityList.clear();
        cityList.addAll(provinceList.get(index).getChildren());
        for (CityBean cityBean : cityList) {
            cityBean.setCheck(false);
        }
        cityIndex = 0;
        cityList.get(cityIndex).setCheck(true);
        cityAdapter.notifyDataSetChanged();
        countySelect(0);
    }

    @SuppressLint("NotifyDataSetChanged")
    private void countySelect(int index) {
        countyList.clear();
        countyList.add(new CityBean("全市"));
        countyList.addAll(cityList.get(index).getChildren());
        for (CityBean cityBean : countyList) {
            cityBean.setCheck(false);
        }
        countIndex = 0;
        if (!isMultipleChoice) {
            countyList.get(countIndex).setCheck(true);
        }
        countyAdapter.notifyDataSetChanged();
    }


    public String getData() {
        StringBuilder stringBuilder = new StringBuilder();
        if (isMultipleChoice) {
            stringBuilder.append(provinceList.get(provinceIndex).getCity_name());
            stringBuilder.append(",");
            stringBuilder.append(cityList.get(cityIndex).getCity_name());
            for (CityBean cityBean : countyList) {
                if (cityBean.isCheck()) {
                    stringBuilder.append(",");
                    stringBuilder.append(cityBean.getCity_name());
                }
            }
        } else {
            stringBuilder.append(provinceList.get(provinceIndex).getCity_name());
            stringBuilder.append(",");
            stringBuilder.append(cityList.get(cityIndex).getCity_name());
            stringBuilder.append(",");
            stringBuilder.append(countyList.get(countIndex).getCity_name());
        }
        return stringBuilder.toString();
    }
}
